package homework14;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.Scanner;

public class Work2 {

    public static void main(String[] args)  {

        ArrayList<String> al = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        String str = null;
        FileWriter fw = null;
        try {
            fw = new FileWriter("f:/abc.txt");
        } catch (IOException e) {
            e.printStackTrace();
        }
        while (true) {
            //不断提醒用户输入内容：
            System.out.println("请输入您要发送的内容：");
            str = sc.next();
            //判断输入内容是否为“end”
            if("end".equals(str)) {
                System.out.println("输入结束！");
                break;
            }
            al.add(str);
        }
        //按字典顺序逆序排序
        Collections.sort(al, Collections.reverseOrder());
        //录入到abc.txt文件当中
        //方法一: for循环
       /* for(int i = 0; i < al.size(); i++) {
            try {
                fw.write(al.get(i) + " ");
                System.out.println(al.get(i));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }*/

        //方法二: for each
        /*for (String s : al) {
            try {
                fw.write(s + " ");
                System.out.println(s);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }*/

        //方法三: 迭代器
        Iterator it = al.iterator();
        while(it.hasNext()) {
            try {
                fw.write((String)it.next() + " ");
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        if (null != fw) {
            try {
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
